#region File Description
//-----------------------------------------------------------------------------
// OptionsMenuScreen.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.GamerServices;
using ScreenManagement;
#endregion

namespace ScreenManagement
{
    /// <summary>
    /// The options screen is brought up over the top of the main menu
    /// screen, and gives the user a chance to configure the game
    /// in various hopefully useful ways.
    /// </summary>
    class OptionsMenuScreen : MenuScreen
    {
        #region Fields

        MenuEntry numPlayerMenuEntry;
        MenuEntry controllingPlayerColorMenuEntry;
        MenuEntry gameVariantMenuEntry;
        MenuEntry difficultyMenuEntry;

        enum NumPlayers
        {
            One,
            Two,
        }

        static NumPlayers currentNumPlayers = NumPlayers.One;
        static HexChessBoard.PieceColor currentColor = HexChessBoard.PieceColor.White;
        static HexChessBoard.GameVariant currentVariant = HexChessBoard.GameVariant.Glinsky;
        static HexChessBoard.Difficulty currentDifficulty = HexChessBoard.Difficulty.Medium;

        #endregion

        #region Initialization


        /// <summary>
        /// Constructor.
        /// </summary>
        public OptionsMenuScreen()
            : base("Options")
        {
            // Create our menu entries.
            numPlayerMenuEntry = new MenuEntry(string.Empty);
            controllingPlayerColorMenuEntry = new MenuEntry(string.Empty);
            gameVariantMenuEntry = new MenuEntry(string.Empty);
            difficultyMenuEntry = new MenuEntry(string.Empty);

            SetMenuEntryText();

            MenuEntry start = new MenuEntry("Start");
            MenuEntry back = new MenuEntry("Back");

            // Hook up menu event handlers.
            numPlayerMenuEntry.Selected += NumPlayerMenuEntrySelected;
            controllingPlayerColorMenuEntry.Selected += ControllingPlayerColorMenuEntrySelected;
            gameVariantMenuEntry.Selected += GameVariantMenuEntrySelected;
            difficultyMenuEntry.Selected += DifficultyMenuEntrySelected;
            start.Selected += PlayGameMenuEntrySelected;
            back.Selected += OnCancel;
            
            // Add entries to the menu.
            MenuEntries.Add(numPlayerMenuEntry);
            MenuEntries.Add(controllingPlayerColorMenuEntry);
            MenuEntries.Add(gameVariantMenuEntry);
            MenuEntries.Add(difficultyMenuEntry);
            MenuEntries.Add(start);
            MenuEntries.Add(back);
        }


        /// <summary>
        /// Fills in the latest values for the options screen menu text.
        /// </summary>
        void SetMenuEntryText()
        {
            numPlayerMenuEntry.Text = "Number of Players: " + currentNumPlayers;
            controllingPlayerColorMenuEntry.Text = "Player 1 Color: " + currentColor;
            gameVariantMenuEntry.Text = "Variant: " + currentVariant;
            difficultyMenuEntry.Text = "Difficulty: " + currentDifficulty;
        }


        #endregion

        #region Handle Input


        /// <summary>
        /// Event handler for when the Ungulate menu entry is selected.
        /// </summary>
        void NumPlayerMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            currentNumPlayers++;

            if (currentNumPlayers > NumPlayers.Two)
                currentNumPlayers = 0;

            SetMenuEntryText();
        }

        /// <summary>
        /// Event handler for when the Language menu entry is selected.
        /// </summary>
        void ControllingPlayerColorMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            currentColor++;

            if (currentColor > HexChessBoard.PieceColor.White)
                currentColor = 0;

            SetMenuEntryText();
        }

        /// <summary>
        /// Event handler for when the Frobnicate menu entry is selected.
        /// </summary>
        void GameVariantMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            currentVariant++;

            if (currentVariant > HexChessBoard.GameVariant.Shafran)
                currentVariant = 0;

            SetMenuEntryText();
        }

        /// <summary>
        /// Event handler for when the Elf menu entry is selected.
        /// </summary>
        void DifficultyMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            currentDifficulty++;

            if (currentDifficulty > HexChessBoard.Difficulty.Hard)
                currentDifficulty = HexChessBoard.Difficulty.Easy;

            SetMenuEntryText();
        }

        /// <summary>
        /// Event handler for when the Play Game menu entry is selected.
        /// </summary>
        void PlayGameMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
#if XBOX
            if ((currentVariant == HexChessBoard.GameVariant.Shafran || currentVariant == HexChessBoard.GameVariant.McCooey) && Guide.IsTrialMode)
            {
                SignedInGamer gamer = Gamer.SignedInGamers[(PlayerIndex)ControllingPlayer];
                if (gamer != null)
                {
                    if (gamer.Privileges.AllowPurchaseContent)
                    {
                        Guide.ShowMarketplace((PlayerIndex)ControllingPlayer);
                    }
                    else
                    {
                        MessageBoxScreen useAllowContentBox = new MessageBoxScreen("Please use an account with purchase privileges\nto play this version!");
                        ScreenManager.AddScreen(useAllowContentBox, ControllingPlayer);
                    }
                }
                else
                {
                    //sign in and purchase message box
                    //Guide.ShowSignIn(1, false);
                    MessageBoxScreen signInBox = new MessageBoxScreen("Please sign in to an account with purchase privileges\nto play this version!");
                    ScreenManager.AddScreen(signInBox, ControllingPlayer);
                }
            }
            else
            {
                if ((int)currentNumPlayers == 0)
                    LoadingScreen.Load(ScreenManager, true, e.PlayerIndex, false, new GameplayScreen(null, currentVariant, (int)currentNumPlayers + 1, currentColor, currentDifficulty, PlayerIndex.One));
                else
                {
                    MessageBoxScreen secondPlayerMessageBox = new MessageBoxScreen("Please hit 'A' on the second player's controller:");
                    secondPlayerMessageBox.Accepted += ConfirmQuitMessageBoxAccepted;
                    ScreenManager.AddScreen(secondPlayerMessageBox, null);
                }
            }
#else
            if ((int)currentNumPlayers == 0)
                LoadingScreen.Load(ScreenManager, true, e.PlayerIndex, false, new GameplayScreen(null, currentVariant, (int)currentNumPlayers + 1, currentColor, currentDifficulty, PlayerIndex.One));
            else
            {
                MessageBoxScreen secondPlayerMessageBox = new MessageBoxScreen("Please hit 'A' on the second player's controller:");
                secondPlayerMessageBox.Accepted += ConfirmQuitMessageBoxAccepted;
                ScreenManager.AddScreen(secondPlayerMessageBox, null);
            }
#endif
        }

        public override void HandleInput(HexChessBoard.PlayerComponents.InputState input, GameTime gameTime)
        {
          
            if (input.CurrentKeyboardStates[(int)this.ControllingPlayer].IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Back) || input.CurrentGamePadStates[(int)this.ControllingPlayer].IsButtonDown(Microsoft.Xna.Framework.Input.Buttons.B) || input.CurrentGamePadStates[(int)this.ControllingPlayer].IsButtonDown(Microsoft.Xna.Framework.Input.Buttons.Back))
            {
                this.ScreenManager.currentBackground = 0;
                this.ExitScreen();
            }

            base.HandleInput(input, gameTime);
        }

        void ConfirmQuitMessageBoxAccepted(object sender, PlayerIndexEventArgs e)
        {
            LoadingScreen.Load(ScreenManager, true, this.ControllingPlayer, false, new GameplayScreen(null, currentVariant, (int)currentNumPlayers + 1, currentColor, currentDifficulty, e.PlayerIndex));
        }

        #endregion
    }
}
